package com.rpms.util;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.WebApplicationContext;

import com.csd.helper.Tool;
import com.csd.pojo.ProjectType;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExcelExport {
	private int pagesize = 50000;
	private WorkbookSettings workbookSettings = new WorkbookSettings();
	protected final Log logger = LogFactory.getLog(getClass());

	public void write(String name,List<String> columnNames,List list) throws Exception {
		
		// WritableSheet writableSheet = writableWorkbook.createSheet("???",0);
		String path  = Tool.getPath()+"userfile"+File.separator+"excel"+File.separator;
		
		
		if(!(new File(path).isDirectory()))
		{
			new File(path).mkdirs();
			
		}
		
		
		workbookSettings.setLocale(Locale.getDefault());
		final WritableWorkbook writableWorkbook = Workbook.createWorkbook(
				new File(path+name), workbookSettings);
		
		int page = 1;
		WritableSheet writableSheet = writableWorkbook.createSheet("第"+page+"页", 0);
		
		int columnCount = list.size();
		
		int row = 0;
		
		for (int j = 0; j <columnNames.size() ; j++) {
			writableSheet.addCell(new Label(j , row, columnNames.get(j)));
		}
		row = 1;
		
		for (int i = 0; i < columnCount; i++) {
			System.out.println("list" + list);
			Object element = list.get(i);
			Class cls = element.getClass();
			Field fieldlist[] = cls.getDeclaredFields();
			int  column = 0;
			for (int m = 0; m < fieldlist.length; m++) {
				Field fld = fieldlist[m];
				String fieldName = fld.getName();
				
				//??????
				Class<? extends Object> classType = fld.getType();
				if(classType.isPrimitive() ){
					if(column > 0){
						column--;
					}
					continue;
				}
				//??????
				String stringLetter=fieldName.substring(0, 1).toUpperCase();    
				String getName="get"+stringLetter+fieldName.substring(1);
				Method getMethod=cls.getMethod(getName, new Class[]{}); 
                //??????
				Object value = getMethod.invoke( element , new Object[]{} );
				if(value instanceof Collection){
					if(column > 0){
						column--;
					}
					continue;
				}
       
				if(value == null){
					value = "无数据";
				}
			    System.out.println(value);
				writableSheet.addCell(new Label(column, row, value.toString()));
				column++;
				if (row > pagesize) {
					row = 0;
					writableSheet = writableWorkbook.createSheet("第"
							+ (++page) + "页", 0);
				}
			}
			
			row++;
			
		}
        
		writableWorkbook.write();
		try {
			writableWorkbook.close();
		} catch (WriteException e) {
			logger.error("writableWorkbook.close() error !" + e, e.getCause());
			e.printStackTrace();
		}
		
	}
	
	private static void makeDir(){
		
        System.out.println(Tool.getPath());
	}

	public static void main(String[] args) {
		
		
		ExcelExport.makeDir();
	}
}
